//
//  optionChooseViewController.m
//  ZPSALE
//
//  Created by tmosport on 14-6-30.
//  Copyright (c) 2014年 tmosport. All rights reserved.
//

#import "optionChooseViewController.h"

@interface optionChooseViewController ()
@property(nonatomic,copy)NSArray *dataArr;
@property(nonatomic,copy)NSArray *keyArr;
@property(nonatomic,copy) NSDictionary *typeStrings;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,retain)NSIndexPath* lastIndex;

@end

@implementation optionChooseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItem];
    if (_sourceType == 1) {
        if (self.totalCount > 10000) {
            _typeStrings =@{
                            @(2) : @"在线支付",
                            @(4) : @"银行转账",
                            };
        }else{
            _typeStrings =@{
                            @(1) : @"货到付款",
                            @(2) : @"在线支付",
                            @(4) : @"银行转账",
                            };

        }
                    self.title = @"请选择支付方式";
//        UIView *footerview = [[UIView alloc] init];
//        footerview.backgroundColor = [UIColor clearColor];
//        NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
//        [paragraphStyle setLineSpacing: 3];
//        NSString *placeText = @"货到付款为第三方快递公司代收款，需收取订单金额3%的手续费。如订单退货，退货金额会自动扣除手续费。如无须退货或拒收，则手续费由珍品网承担。为降低不必要的损失，强烈建议您使用在线支付。";
//        NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
//        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:placeText attributes:attributes];
//        UILabel *tip = [[UILabel alloc] initWithFrame:CGRectMake(18, 16, 100, 20)];
//        tip.textColor = ColorFromRGB(0x666666);
//        tip.text = @"温馨提示：";
//        tip.font =  [UIFont fontWithName:@"Heiti SC" size:12];
//        UITextView *desc = [[UITextView alloc]initWithFrame:CGRectMake(12, 40, 302, 200)];
//        desc.attributedText = attributedString;
//        desc.textColor =ColorFromRGB(0x666666);
//        [footerview addSubview:tip];
//        [footerview addSubview:desc];
//        self.optionTV.tableFooterView = footerview;
        
    }else{
        _typeStrings =@{
                        @(1) : @"只工作日送货",
                        @(2) : @"只双休日、假日送货",
                        @(3) : @"工作日、休息日均可送货",
                                            };
        self.title = @"请选择配送方式";
    }
     _dataArr = [_typeStrings allValues];
     _keyArr = [_typeStrings allKeys];

}
-(void)setNavigationItem {
    [self.navigationController.navigationBar setBackIndicatorImage:
     [UIImage imageNamed:@"return"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
     [UIImage imageNamed:@"return"]];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}

-(void)leftBarbuttonItemClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
    if ([[_keyArr objectAtIndex:indexPath.row] intValue] == _chooseId ) {
        self.lastIndex = indexPath;
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    cell.textLabel.text =[_dataArr objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    _index = [[_keyArr objectAtIndex:indexPath.row] intValue];
    if (_sourceType == 1) {
            NSDictionary *payTypeDic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:_index], @"id", [_dataArr objectAtIndex:indexPath.row],@"payTpyeTitle", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changePayType" object:payTypeDic];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        NSDictionary *shippingDic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:_index], @"id", [_dataArr objectAtIndex:indexPath.row],@"shippingTitle", nil];
         [[NSNotificationCenter defaultCenter] postNotificationName:@"changeShippingTime" object:shippingDic];
        [self.navigationController popViewControllerAnimated:YES];
    }

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndex];
    oldCell.accessoryType = UITableViewCellAccessoryNone;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.lastIndex = indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
